//
//  YZAskViewController.m
//  FastDoctor
//
//  Created by yongzhen on 16/9/8.
//  Copyright © 2016年 yongzhen. All rights reserved.
//

#import "YZAskViewController.h"
#import "Masonry.h"
#import "UMSocial.h"

@interface YZAskViewController ()<UITableViewDelegate,UIAlertViewDelegate>
@property(nonatomic,weak)UIImageView *imageV;
@property (weak, nonatomic) UITableView *tableView;
@property (weak, nonatomic) UITextField *textField;
@property(nonatomic,weak)UIVisualEffectView *blurView;
@property(nonatomic,weak)UIButton *sendButton;
@property (nonatomic,assign)CGFloat transform;
@property(nonatomic,weak)UIImageView *headView;
@property(nonatomic,weak)UIView *chatView;
@property(nonatomic,weak)UILabel *chatLabel;



@end

@implementation YZAskViewController


- (void)viewDidLoad {
    [super viewDidLoad];
        self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.hidden = NO;
    self.navigationItem.title = @"专家咨询";
    
    
    self.tableView.delegate = self;
    
    [self.tableView registerClass:[YZAskViewController class] forCellReuseIdentifier:@"chatID"];
    
    
//    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWilShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWIlhide:) name:UIKeyboardWillHideNotification object:nil];
    self.transform = 0;
    
    
}
-(void)keyboardWIlhide:(NSNotification *)user
{
      [UIView animateWithDuration:.6 animations:^{
//        self.sendButton.frame = CGRectMake(0, 0, 200, 200);
        
   }];
    
}

-(void)setupUI
{
    
//    UIImage *image =[UIImage imageNamed:_image.description];
    UIImageView *imageV = [[UIImageView alloc] initWithImage:_image];
    [self.view addSubview:imageV];
    [imageV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(8);
        make.top.equalTo(self.view).offset(100);
        make.width.equalTo(@60);
        make.height.equalTo(@60);
    }];
    imageV.layer.cornerRadius = 30;
    imageV.layer.masksToBounds = YES;
    
    
    UILabel *askLabel = [[UILabel alloc] init];
    askLabel.tintColor = [UIColor blackColor];
    askLabel.text = @"您好，请问有什么需要帮助的吗";
    [askLabel sizeToFit];
    [self.view addSubview:askLabel];
    [askLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(imageV);
        make.left.mas_equalTo(imageV.mas_right).offset(10);
    }];
    
//    UITableView *tableView = [[UITableView alloc]init];
//    [self.view addSubview:tableView];
//    self.tableView = tableView;
    
    // 聊天框
    CGFloat blurHeight = 44;
    
    self.tableView.contentInset = UIEdgeInsetsMake(0, 0, blurHeight+5, 0);
    
    //对比记忆Layout和flowLayout
    UIBlurEffect *effect = [[UIBlurEffect alloc]init];
    
    //毛玻璃效果
    UIVisualEffectView *blurView = [[UIVisualEffectView alloc]initWithEffect:effect];
    [self.view addSubview:blurView];
    [blurView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.bottom.equalTo(self.view).offset(-self.transform);
        make.height.mas_equalTo(44);
    }];
    
    UITextField *textField = [[UITextField alloc]init];
//        textField.backgroundColor = [UIColor redColor];
    //设置textfield的边框
    textField.borderStyle = UITextBorderStyleRoundedRect;
    [blurView.contentView addSubview:textField];
    self.textField = textField;
    
    UIButton *sendButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [sendButton setTitle:@"发送" forState:UIControlStateNormal];
    [blurView.contentView addSubview:sendButton];
    // 下面是正确的
    [sendButton addTarget:self action:@selector(sendBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    //下面登陆的
//    [sendButton addTarget:self action:@selector(sendBtnClicdenglu:) forControlEvents:UIControlEventTouchUpInside];
    
    [textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(blurView.contentView).offset(8);
        make.top.equalTo(blurView.contentView).offset(4);
        make.bottom.equalTo(blurView.contentView).offset(-4);
    }];
    
    [sendButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(blurView.contentView);
        make.right.equalTo(blurView.contentView).offset(-8);
        make.left.equalTo(textField.mas_right).offset(8);
        make.width.mas_equalTo(40);
    }];
    
    self.sendButton = sendButton;
    _imageV = imageV;
    self.blurView = blurView;

    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveKeyboardFrameNotification:) name:UIKeyboardWillChangeFrameNotification object:nil];
    
    
}
- (void)receiveKeyboardFrameNotification:(NSNotification *)notification{
//    NSLog(@"notification = %@",notification);
//     NSLog(@"b111-lurview - %@",self.sendButton);
    CGRect endframe = [notification.userInfo[@"UIKeyboardCenterEndUserInfoKey"]CGRectValue];
    // 计算屏幕偏移量
    CGFloat transform = [UIScreen mainScreen].bounds.size.height - endframe.origin.y;
    // 屏幕偏移
    [UIView animateWithDuration:0.25 animations:^{

        self.blurView.transform = CGAffineTransformMakeTranslation(0, -129 *2);
        self.transform = transform;

    } completion:^(BOOL finished) {
//        NSLog(@"b222-lurview - %@",self.sendButton);
    }];
//     NSLog(@"222---notification = %@",notification);
   
}
// 发送的点击事件




-(void)sendBtnClick:(UIButton *)Btn;
{
    NSString *chatStr = [NSString stringWithFormat:@"%@",self.textField.text];
    UIView *chatV = [self chatViewwithString2:chatStr];
    [self.view addSubview:chatV];
    [self viewDidLoad];
    self.chatView = chatV;
//    [self setupUI];
    [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(answer) userInfo:nil repeats:NO];
   
  
  
}
-(void)answer
{
   
       self.textField.text = nil;
        UIAlertView *view = [[UIAlertView alloc]initWithTitle:@"我下班了，不接受咨询，哈哈哈哈哈哈，清除你的聊天记录，让你投诉不了，哈哈" message:@"" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"你叼", nil];
    view.delegate = self;
        [view show];
}
//代理方法
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    [self.chatView removeFromSuperview];
    
    
}
// 聊天对话框
-(UIView *)chatViewwithString2:(NSString *)string
{
    
    //自定义聊天内容
    CGRect rect = [string boundingRectWithSize:CGSizeMake(200, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:17]} context:nil];
    
    
    //    聊天的内容
    UILabel *lab = [[UILabel alloc]initWithFrame:CGRectMake(12, 208, rect.size.width+5, rect.size.height+20)];
    
    lab.text = string;
    lab.numberOfLines = 0;
    lab.textColor = [UIColor darkGrayColor];
    lab.font = [UIFont systemFontOfSize:14];
       lab.backgroundColor = [UIColor clearColor];
    
    
    //头像
    int headviewX = [UIScreen mainScreen].bounds.size.width / 2 - 55;
    UIImageView *headView = [[UIImageView alloc]initWithFrame:CGRectMake(headviewX + 10, 208, 50, 50)];
     headView.image = [UIImage imageNamed:@"图层-2"];
    self.headView = headView;
    headView.layer.cornerRadius = 25.f;
    headView.layer.masksToBounds = YES;
    headView.layer.borderWidth = 0.f;
    headView.layer.borderColor = [[UIColor colorWithRed:80/255.0 green:227/255.0 blue:194/255.0 alpha:1]
 CGColor];
    //    获取到拉伸后的图片
    int ImageX = [UIScreen mainScreen].bounds.size.width /2-CGRectGetWidth(headView.frame)-CGRectGetWidth(lab.frame)-10;
    UIImageView *bgImageView = [[UIImageView alloc]initWithFrame:CGRectMake(ImageX, 0, lab.frame.size.width+20, lab.frame.size.height+10)];
       //创建左右视图
    int x = [UIScreen mainScreen].bounds.size.width/2-15;
    
    UIView *chatView = [[UIView alloc]initWithFrame:CGRectMake(x, 0, 220, bgImageView.frame.size.height)];
    chatView.tag = 1;
    [chatView addSubview:bgImageView];
  
    [bgImageView addSubview:lab];
   
    [chatView addSubview:headView];
    self.headView = headView;
    self.chatLabel = lab;
    
    self.textField.text = nil;
    
    return chatView;
    
}

-(UIView *)chatViewWithString:(NSString *)chatStr
{
    
    // 头像
    UIImageView *headView = [[UIImageView alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width / 2 - 45,0, 50, 50)];
    headView.image = [UIImage imageNamed:@"图层-2"];
    self.headView = headView;
    [self.view addSubview:headView];
    
    // 聊天内容
    CGRect rect = [chatStr  boundingRectWithSize:CGSizeMake(200, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:17]} context:nil];
//    UILabel *label = [[UILabel alloc] init];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(22, 8, rect.size.width+5, rect.size.height+20)];
    label.text = chatStr;
    label.numberOfLines = 0;
    label.font = [UIFont systemFontOfSize:14];
    label.textColor = [UIColor grayColor];
    label.backgroundColor = [UIColor clearColor];
    [label sizeToFit];
    self.chatLabel = label;
    [self.view addSubview:label];
//    label.frame.size.width = rect.size.width+5;
    
    
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(headView.mas_left).offset(-10);
        make.centerX.mas_equalTo(headView);
    }];
    
    
    //创建左右视图
    int x = [UIScreen mainScreen].bounds.size.width/2-15;
    
    
    UIView *chatView = [[UIView alloc]initWithFrame:CGRectMake(x, 160, 220, label.frame.size.height + 10)];
    chatView.tag = 1;
//    [chatView addSubview:bgImageView];
        [chatView addSubview:label];
//    [bgImageView addSubview:lab];
    //    [imageView addSubview:lab];
    [chatView addSubview:headView];
    self.textField.text = nil;
    
    
    
    
    
    return chatView;
}



//-(BOOL)navigationShouldPopOnBackButton{
//    
//    UIAlertView *view = [[UIAlertView alloc]initWithTitle:@"您真的要离开么？" message:@"" delegate:self cancelButtonTitle:@"再等等" otherButtonTitles:@"走啦", nil];
//    [view show];
//    
//    return NO;
//}
//- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
//    
//    if (buttonIndex == 1) {
//        [self.navigationController popViewControllerAnimated:YES];
//    }
//}
-(void)setImage:(UIImage *)image
{
    _image = image;

    [self setupUI];
}
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [self.view endEditing:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)dealloc
{
     [[NSNotificationCenter defaultCenter] removeObserver:self];
}
@end
